//
//  MarketFlowTableViewCell.m
//  BondWatch
//
//  Created by Jason Yuan on 12/1/15.
//  Copyright © 2015 Jason Yuan. All rights reserved.
//

#import "MarketFlowTableViewCell.h"

@implementation MarketFlowTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        sectorLabel = [[UILabel alloc] init];
        sectorLabel.textAlignment = NSTextAlignmentLeft;
        sectorLabel.font = ARIALBOLD10;
        
        volumeLabel = [[UILabel alloc] init];
        volumeLabel.textAlignment = NSTextAlignmentRight;
        volumeLabel.font = ARIAL10;
        
        ratioLabel = [[UILabel alloc] init];
        ratioLabel.textAlignment = NSTextAlignmentRight;
        ratioLabel.font = ARIAL10;
        
        averageLabel = [[UILabel alloc] init];
        averageLabel.textAlignment = NSTextAlignmentRight;
        averageLabel.font = ARIAL10;
        
        changeLabel = [[UILabel alloc] init];
        changeLabel.textAlignment = NSTextAlignmentRight;
        changeLabel.font = ARIAL10;
        
        dealerPercentageLabel = [[UILabel alloc] init];
        dealerPercentageLabel.textAlignment = NSTextAlignmentRight;
        dealerPercentageLabel.font = ARIAL10;
        
        [self addSubview:sectorLabel];
        [self addSubview:volumeLabel];
        [self addSubview:ratioLabel];
        [self addSubview:averageLabel];
        [self addSubview:changeLabel];
        [self addSubview:dealerPercentageLabel];
    }
    return self;
}

- (void)setUIFrame {
    if ([UIApplication sharedApplication].statusBarOrientation == UIDeviceOrientationLandscapeLeft ||
        [UIApplication sharedApplication].statusBarOrientation == UIDeviceOrientationLandscapeRight) {
        [sectorLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(2);
            make.centerY.equalTo(self);
            make.width.mas_equalTo(120);
        }];
        
        [volumeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(sectorLabel.mas_right).offset(5);
            make.centerY.equalTo(self);
            make.width.mas_equalTo(80);
        }];
        
        [ratioLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(volumeLabel.mas_right).offset(5);
            make.centerY.equalTo(self);
            make.width.mas_equalTo(80);
        }];
        
        [averageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(ratioLabel.mas_right).offset(5);
            make.centerY.equalTo(self);
            make.width.mas_equalTo(80);
        }];
        
        [changeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(averageLabel.mas_right).offset(5);
            make.centerY.equalTo(self);
            make.width.mas_equalTo(80);
        }];
        
        [dealerPercentageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(changeLabel.mas_right).offset(5);
            make.centerY.equalTo(self);
            make.width.mas_equalTo(80);
        }];
    } else {
        [sectorLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(2);
            make.centerY.equalTo(self);
            make.width.mas_equalTo(120);
        }];
        
        [volumeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(sectorLabel.mas_right).offset(5);
            make.centerY.equalTo(self);
            make.width.mas_equalTo(80);
        }];
        
        [ratioLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(volumeLabel.mas_right).offset(5);
            make.centerY.equalTo(self);
            make.width.mas_equalTo(80);
        }];
        
        [averageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(ratioLabel.mas_right).offset(5);
            make.centerY.equalTo(self);
            make.width.mas_equalTo(80);
        }];
        
        [changeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(averageLabel.mas_right).offset(5);
            make.centerY.equalTo(self);
            make.width.mas_equalTo(80);
        }];
        
        [dealerPercentageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(changeLabel.mas_right).offset(5);
            make.centerY.equalTo(self);
            make.width.mas_equalTo(80);
        }];
    }
}

- (void)setLabelText:(NSDictionary *)data {
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    formatter.numberStyle = NSNumberFormatterDecimalStyle;
    
    NSString *sector = [data objectForKey:@"sector"];
    sectorLabel.text = sector;
    
    NSNumber *volume = [data objectForKey:@"volume"];
    NSString *formatterVolume = [formatter stringFromNumber:volume];
    volumeLabel.text = formatterVolume;
    
    NSNumber *ratio = [data objectForKey:@"buySellRatio"];
    ratioLabel.text = [NSString stringWithFormat:@"%.2f", [ratio floatValue]];
    
    NSNumber *average = [data objectForKey:@"volumeAverage"];
    NSString *formatterAverage = [formatter stringFromNumber:average];
    averageLabel.text = formatterAverage;
    
    NSNumber *change = [data objectForKey:@"volumeChange"];
    changeLabel.text = [NSString stringWithFormat:@"%.2f%%", [change floatValue]];
    
    NSNumber *dealerPercentage = [data objectForKey:@"dealerPercentage"];
    dealerPercentageLabel.text = [NSString stringWithFormat:@"%.2f%%", [dealerPercentage floatValue]];
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
